//
//  FKChooseAddressVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/19.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKChooseAddressVC.h"
#import "FKChooseAddressCell.h"
#import "FKAddAddressVC.h"

@interface FKChooseAddressVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;

@end

@implementation FKChooseAddressVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择收货地址";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"refreshAddressList" object:nil];
    
    UIButton *addBtn = [UIButton fk_btnWithTarget:self action:@selector(addBtnClicked) font:fk_adjustFont(15) normalTitle:@"添加收货地址" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
    [self.view addSubview:addBtn];
    
    UITableView *tableView  = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.mas_equalTo(49);
    }];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.equalTo(addBtn.mas_top);
    }];
    
    [self loadData];
}

- (void)addBtnClicked
{
    FKAddAddressVC *vc = [[FKAddAddressVC alloc] init];
    fkPushNav(vc);
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKChooseAddressCell *cell = [FKChooseAddressCell cellWith:tableView];
    cell.data = self.datas[indexPath.section];
    return cell;
}

#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(112);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *address = self.datas[indexPath.section];
    if (self.selectedAddressBlock) {
        self.selectedAddressBlock(address);
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"selectedAddressNoti" object:nil userInfo:@{@"address" : address}];
    fkPopVC;
}


#pragma mark - 网络请求
- (void)loadData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"page"] = @"1";
    param[@"pagesize"] = @"100";
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=address_list" param:param success:^(FKBaseResult *result) {
//        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            self.datas = result.data[@"list"];
            if (self.addressId) {
                NSMutableArray *array = [NSMutableArray array];
                for (NSDictionary *address in self.datas) {
                    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:address];
                    if ([self.addressId isEqualToString:address[@"id"]]) {
                        dict[@"selected"] = @(true);
                    }
                    [array addObject:dict];
                }
                self.datas = array;
            }
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
//        [self.tableView.mj_header endRefreshing];
    }];
}


@end
